package com.mangguo.tingjian.content.controller;


import com.mangguo.tingjian.common.Result;
import com.mangguo.tingjian.content.controller.rsp.TagConfRsp;
import com.mangguo.tingjian.content.entity.TagConf;
import com.mangguo.tingjian.content.service.ITagConfService;
import com.mangguo.tingjian.mgtjv3.service.ICategoryMenuService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 * 内容数据页tag配置 前端控制器
 * </p>
 *
 * @author auto
 * @since 2019-11-24
 */
@RestController
@RequestMapping("/content/tag-conf")
@AllArgsConstructor
public class TagConfController {

    private final ITagConfService tagConfService;

    private final ICategoryMenuService categoryMenuService;

    @GetMapping("/{type}")
    public Result<List<TagConfRsp>> getTagConfByType(@PathVariable("type") Integer type){
        Map<Long, List<TagConf>> map = new HashMap<>();
        Map<Long, String> menuNameMap = new HashMap<>();
        categoryMenuService.getAllCategoryMenu().forEach(e -> {
            map.computeIfAbsent(e.getMenuId(), k -> {
                List<TagConf> tagConfs = new ArrayList<>();
                TagConf tagConf = new TagConf();
                tagConf.setTagId(-1L);
                tagConf.setTagName("全部");
                tagConf.setParentTagId(e.getMenuId());
                tagConf.setTagLvl(2);
                tagConfs.add(tagConf);
                return tagConfs;
            });
            if(e.getMenuId().equals(e.getSubMenuId())){
                map.get(e.getMenuId()).add(1,TagConf.builder().tagLvl(2).tagId(e.getSubMenuId()).tagName("推荐").parentTagId(e.getMenuId()).build());
            }else if(e.getSubMenuId() != null){
                map.get(e.getMenuId()).add(TagConf.builder().tagLvl(2).tagId(e.getSubMenuId()).tagName(e.getSubname()).parentTagId(e.getMenuId()).build());
            }
            menuNameMap.put(e.getMenuId(), e.getName());
        });
        List<TagConfRsp> list = new LinkedList<>();
        menuNameMap.forEach((id, name) -> {
            TagConfRsp tagConfRsp = new TagConfRsp();
            tagConfRsp.setTagId(id);
            tagConfRsp.setTagName(name);
            if(map.get(id).size() == 1){
                TagConf tagConf = new TagConf();
                tagConf.setTagId(0L);
                tagConf.setTagName(name);
                tagConf.setParentTagId(id);
                tagConf.setTagLvl(2);
                map.get(id).add(tagConf);
            }
            tagConfRsp.setChildTags(map.get(id));
            list.add(tagConfRsp);
        });

        TagConfRsp tagConfRsp = new TagConfRsp();
        tagConfRsp.setTagId(-1L);
        tagConfRsp.setTagName("全部");
        TagConf tagConf = new TagConf();
        tagConf.setTagId(-1L);
        tagConf.setTagName("全部");
        tagConf.setParentTagId(-1L);
        tagConf.setTagLvl(2);
        tagConfRsp.setChildTags(Collections.singletonList(tagConf));
        list.add(0, tagConfRsp);

        return Result.success(list);
    }
}
